﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.Samples.ServiceHosting.StorageClient;
using System.Data.Services.Client;


namespace CarnetAnniversaires.Metier
{

    public class Anniversaire : TableStorageEntity
    {
        // User defined properties
        public string nom { get; set; }
        public string prenom { get; set; }
        public int annee { get; set; }
        public string categorie { get; set; } // Champ libre

        public Anniversaire()
        {
            // User ID
            PartitionKey = "EMPTY";

            // Mois_Jour
            RowKey = "EMPTY";
        }

        public Anniversaire(string in_userId, int in_mois, int in_jour, string in_nom, string in_prenom)
        {

            // User ID
            PartitionKey = in_userId;

            // Mois_Jour
            RowKey = GetFromKeyFromMoisEtJour(in_mois, in_jour);

            nom = in_nom;
            prenom = in_prenom;
        }

        
        internal static string GetMoisFromRowKey(string in_rowKey)
        {
            return in_rowKey.Split('_')[0];
        }

        internal static string GetJourFromRowKey(string in_rowKey)
        {
            return in_rowKey.Split('_')[1];
        }

        internal static string GetFromKeyFromMoisEtJour(int in_mois, int in_jour)
        {
            return string.Format("{0}_{1}", in_mois, in_jour);;
        }
    }


    public class AnniversaireService : TableStorageDataServiceContext
    {
        private static AnniversaireService singleton = null;

        public static AnniversaireService GetInstance()
        {
            if (singleton == null)
            {
                StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
                singleton = new AnniversaireService(accountInfo);
                singleton.Initialize();
            }

            return singleton;
        }


        public AnniversaireService(StorageAccountInfo accountInfo)
            : base(accountInfo)
        {
        }

        public IQueryable<Anniversaire> Anniversaires
        {
            get
            {
                return this.CreateQuery<Anniversaire>("Anniversaires");
            }
        }


        public Anniversaire CreerAnniversaire(string in_userId, int in_mois, int in_jour, string in_nom, string in_prenom, int in_annee, string in_categorie)
        {
            
            var l_created = new Anniversaire(in_userId, in_mois, in_jour, in_nom, in_prenom);
            l_created.annee = in_annee;
            l_created.categorie = in_categorie;
            this.AddObject("Anniversaires", l_created);
            this.SaveChanges();

            return l_created;
        }

        public void Initialize()
        {
            // Retry Policy
            RetryPolicy = RetryPolicies.RetryN(3, TimeSpan.FromSeconds(1));

            // Dynamically create the tables if necessary
            // Should be called only once for perf reasons
            StorageAccountInfo accountInfo = StorageAccountInfo.GetAccountInfoFromConfiguration("TableStorageEndpoint");
            TableStorage.CreateTablesFromModel(typeof(AnniversaireService), accountInfo);

            // [PENDING]
            //}
            //catch (DataServiceRequestException ex)
            //{
            //    statusMessage = "Unable to connect to the table storage server. Please check that the service is running.<br>"
            //                    + ex.Message;
            //}

        }
    }
}
